package com.swt.gap.controller;

import com.swt.gap.common.exception.NoMatchedTemplateException;
import com.swt.gap.common.graph.ImportEmailGraphData;
import com.swt.gap.model.vo.graph.ImportGraphDataVO;
import com.swt.gap.model.vo.graph.PreviewGraphDataMultiFileVO;
import com.swt.gap.model.vo.graph.PreviewGraphDataVO;
import com.swt.gap.service.GraphDataTransferService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.format.DateTimeParseException;

/**
 * 转换图数据相关接口
 *
 * @author sx
 * @date 2020/7/20 16:57
 */
@Api(tags = "图谱数据v2")
@RestController
@RequestMapping("/graph-data/v2")
@Slf4j
public class GraphDataTransferController {

    private final GraphDataTransferService graphDataTransferService;

    public GraphDataTransferController(GraphDataTransferService graphDataTransferService) {
        this.graphDataTransferService = graphDataTransferService;
    }

    @PostMapping("/graph-preview")
    @ApiOperation(value = "图谱预览", notes = "图谱预览")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity<Object> previewGraphData(@RequestBody PreviewGraphDataVO record) {
        try {
            return ResponseEntity.ok(graphDataTransferService.previewGraphData(record));
        } catch (DateTimeParseException e) {
            log.error("日期时间格式错误");
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("日期时间格式错误，请指定正确的格式");
        } catch (NoMatchedTemplateException e2) {
            log.error("表头与模板不匹配");
            e2.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("表头与模板不匹配，请修改模板");
        }
    }

    @PostMapping("/graph")
    @ApiOperation(value = "表单文件和表单数据的关联", notes = "表单文件和表单数据的关联，生成图数据。文本数据结构化成表单数据也可用次接口")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity<Object> transferGraphData(@RequestBody ImportGraphDataVO record) {
        try {
            return ResponseEntity.ok(graphDataTransferService.importGraphData(record));
        } catch (DateTimeParseException e) {
            log.error("日期时间格式错误");
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("日期时间格式错误，请指定正确的格式");
        } catch (NoMatchedTemplateException e2) {
            log.error("表头与模板不匹配");
            e2.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("表头与模板不匹配，请修改模板");
        }
    }

    @PostMapping("/email-graph")
    @ApiOperation(value = "eml图谱关联", notes = "eml图谱关联")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity<Object> importEmlGraphData(@RequestBody ImportEmailGraphData record) {
        return ResponseEntity.ok(graphDataTransferService.importEmlGraphData(record));
    }


    @PostMapping("/email-import")
    @ApiOperation(value = "引入email，这是个假接口", notes = "这是个假接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "fileName", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId", required = true)
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED")})
    public ResponseEntity importKB(@RequestParam String fileName, @RequestParam Integer taskId) throws IOException {

        return ResponseEntity.ok(graphDataTransferService.importKB(taskId, fileName));

    }

}
